<script setup lang="ts">
import { onMounted } from 'vue';
import { state } from './state';
import dayjs from 'dayjs';
import { navigateTo, redirectTo, reLaunch, getCurrentInstance } from '@tarojs/taro';
import { APP_PLAN_TASK_LIST_QUERY } from '/@/api/modules/maintain/plan-task';

/**
 * 获取路由参数
 */
const params = getCurrentInstance().router?.params as {
  assetId: string;
};

/**
 *@description 保养计划列表
 * @param page 当前页
 * @param pageSize -每页条数
 */
function queryList(page: number = 1, pageSize: number = 15) {
  if (!page && state.finished) return;
  state.loading = true;

  APP_PLAN_TASK_LIST_QUERY({
    comQuery: { page, pageSize },
    planType: 2,
    handleStatus: 2,
    assetHandleStatus: 2,
    assetId: params.assetId,
  })
    .then((res) => {
      state.loading = false;
      if (
        state.activePage >=
          (res.comQuery?.pageCount ||
            (res.comQuery?.totalCount as number) / (res.comQuery?.pageSize as number)) ||
        1
      ) {
        state.finished = true;
      }

      if (!res.planTaskDto || !res.planTaskDto.length) {
        state.tipVisible = true;
        return;
      }

      res.planTaskDto.forEach((item) => state.list.push(item));
      state.activePage += 1;

      if (state.list.length === 1) {
        redirectTo({
          url: `/pages/inspection/edit/index?from=maintenance&module=scan&planTaskId=${state.list[0].id}&assetId=${params.assetId}`,
        });
      }
    })
    .catch(() => {
      state.loading = false;
    });
}

/**
 * @function 加载
 */
function onLoading() {
  if (state.finished) return;

  queryList(state.activePage);
}

/**
 * 列表刷新
 */
function refreshList() {
  state.activePage = 1;
  state.loading = true;
  state.finished = false;
  state.list = [];
  onLoading();
}

onMounted(() => {
  refreshList();
});
</script>

<template>
  <view class="maintenance-list app-page--1">
    <app-navbar title="保养任务" />

    <scroll-view
      class="app-page__wrapper"
      :scroll-y="true"
      :lower-threshold="100"
      @scrolltolower="onLoading()"
    >
      <view class="maintenance-list__wrapper">
        <view
          class="maintenance-item"
          v-for="item in state.list"
          :key="item.id || ''"
          @click="
            navigateTo({
              url: `/pages/inspection/edit/index?from=maintenance&module=scan&planTaskId=${item.id}&assetId=${params.assetId}`,
            })
          "
        >
          <view class="maintenance-item__header">
            <view class="maintenance-item__header--left">
              <text class="maintenance-item__title">{{ item.planName }}</text>
            </view>
          </view>
          <view>
            <text>任务编号:&nbsp;&nbsp;</text>
            <text>{{ item.taskCode }}</text>
          </view>

          <view>
            <text>执行时间:&nbsp;&nbsp;</text>
            <text
              >{{ dayjs(item.validTime).format('YYYY-MM-DD HH:mm') }}至{{
                dayjs(item.invalidTime).format('YYYY-MM-DD HH:mm')
              }}</text
            >
          </view>
        </view>
      </view>
      <view class="app-page__list-tips">
        <text v-if="!state.list.length">暂无数据！</text>
        <text v-else-if="state.list.length && state.loading">加载中...</text>
        <text v-else-if="state.list.length && state.finished">加载完成!</text>
      </view>
    </scroll-view>
    <nut-dialog
      no-cancel-btn
      title="温馨提示"
      content="您当前没有需处理的任务"
      v-model:visible="state.tipVisible"
      @ok="
        reLaunch({
          url: `/pages/scan/scan-success/index?assetId=${params.assetId}`,
        })
      "
    />
  </view>
</template>

<style lang="scss">
@import './index.scss';
</style>
